package threadlocal;

import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MakeOutOfMemory {

    static ExecutorService executor = Executors.newFixedThreadPool(10);


    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            executor.execute(()->{
                BigData bigData = new BigData();
              /*  HashMap<Thread, BigData> map = new HashMap<>();
                map.put(Thread.currentThread(), bigData);
                map.get(Thread.currentThread());
                System.out.println(Thread.currentThread().getName()+"执行完成");
*/
                ThreadLocal<BigData> tl = new ThreadLocal();
                tl.set(bigData);
                tl.get();
                tl.remove(); //不使用remove会oom
                System.out.println(Thread.currentThread().getName()+"执行完成");
            });
        }
        executor.shutdown();
    }
}

class BigData{
    private byte[] data = new byte[1024*1024*100]; //50MB
}
